const DataSchema = require('./DataSchema');
const DataQuery = require('./DataQuery');
const DataImporter = require('./DataImporter');

/**
 * DataService - Facade for data operations
 * Aggregates functionality from Schema, Query, and Importer
 */
class DataService {
    // --- Schema Delegates ---
    static async getSchemaFields(dataType) {
        return DataSchema.getSchemaFields(dataType);
    }

    static getTableName(dataType) {
        return DataSchema.getTableName(dataType);
    }

    static async ensureTableExists(knex, tableName, dataType) {
        return DataSchema.ensureTableExists(knex, tableName, dataType);
    }

    static generateRowHash(row, dataType) {
        return DataSchema.generateRowHash(row, dataType);
    }

    // --- Query Delegates ---
    static async queryData(knex, options) {
        return DataQuery.queryData(knex, options);
    }

    // --- Import Delegates ---

    // Legacy support: redirects "importData" (Array-based) to DataImporter.importFromArray
    static async importData(caseKnex, options) {
        return DataImporter.importFromArray(caseKnex, options);
    }

    // Legacy support: deduplicateInBatch
    static deduplicateInBatch(data, dataType) {
        // Enforce hash generation if missing
        data.forEach(row => {
            if (!row.row_hash) row.row_hash = DataSchema.generateRowHash(row, dataType);
        });
        return DataImporter.deduplicateInBatch(data);
    }
}

module.exports = DataService;
